package com.hmdp.utils;


import com.hmdp.dto.UserDTO;
import com.hmdp.entity.User;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by John.Ma on 2022/3/8 0008 23:58
 */
public class LoginInterceptor implements HandlerInterceptor {


    private StringRedisTemplate stringRedisTemplate;

    // 利用构造函数注入
    public LoginInterceptor(StringRedisTemplate stringRedisTemplate){
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // ////////////基于Session
        // HttpSession session = request.getSession();
        //
        // // 2.获取session中的用户
        // Object user = session.getAttribute("user");
        // // 3.判断用户是否存在
        // if(user == null){
        //     //4.不存在,拦截, 返回401状态码
        //     response.setStatus(401);
        //     return false;
        // }
        //
        // //5.存在,保存用户信息到 ThreadLocal
        // UserHolder.saveUser((UserDTO)user);
        // return true;


        ///// 基于redis
        // TODO 1.获取请求头中的token

        // TODO 2.基于TOKEN获取redis用户

        // TODO 将查询到的hash数据转为UserDTO对象

        // TODO 6.存在,保存用户信息到ThreadLocal

        // TODO 7.刷新Token有效期

        // TODO 8. 放行
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 移除用户
        UserHolder.removeUser();
    }
}
